 import javax.microedition.lcdui.*;
import java.lang.Float;
public class CalculatorTool extends Form implements CommandListener{
	private Displayable previousdisplay;
	private Display hienthi;
	private FileManagerMIDlet filemanager;
	private Command tinh, tinhtiep, tinhlai, thoat;
    	private Form formketqua;
    	private TextField sothunhat, sothuhai;
    	private ChoiceGroup pheptinh;
    	private float gtsothunhat, gtsothuhai, ketqua;
    	private String ketquadaydu;	

    	public CalculatorTool(FileManagerMIDlet fm, Displayable previousdisplay, Display dp){
		super("Nhap So Va Chon Phep Tinh");
		this.filemanager = fm;
		this.hienthi = dp;
		this.previousdisplay = previousdisplay;		
		formketqua = new Form("Ket Qua");
		thoat = new Command("Tro ve", Command.BACK, 1);
		tinh = new Command("Tinh", Command.OK, 1);
		tinhtiep = new Command("Tinh Tiep", Command.OK, 2);
		tinhlai = new Command("Tinh Lai",Command.BACK, 2);
		sothunhat = new TextField("",null, 32, TextField.DECIMAL);
		sothuhai = new TextField("",null, 32, TextField.DECIMAL);
		pheptinh = new ChoiceGroup("",Choice.POPUP);
		pheptinh.append("+",null);
		pheptinh.append("-",null);
		pheptinh.append("*",null);
		pheptinh.append("/",null);
		addCommand(thoat);
		addCommand(tinh);
		formketqua.addCommand(tinhtiep);
		formketqua.addCommand(tinhlai);
		formketqua.addCommand(thoat);
		append(sothunhat);
		append(pheptinh);
		append(sothuhai);
		setCommandListener(this);
		formketqua.setCommandListener(this);

	}   	

    public void commandAction(Command c, Displayable d) {
       		 if(c == thoat)
		{
			hienthi.setCurrent(previousdisplay);
		}
                
		if(c == tinh)
		{
			boolean exception = false;
			int a = 0;
			try{
				a =  Integer.parseInt(sothuhai.getString());
			}
			catch(Exception e){
				exception = true;
				filemanager.showAlert("Chuong trinh khong the tinh duoc so co dau phay va cac so qua lon!", this);
			}
			if(sothunhat.size() == 0)
			{
				filemanager.showAlert("Ban phai dien so thu nhat !", this);				
			}
			else if(sothuhai.size() == 0)
			{
				filemanager.showAlert("Ban phai dien so thu hai !", this);				
			}       			
                        		else if((a == 0)&&(pheptinh.getSelectedIndex() == 3))
                        		{
                           			 filemanager.showAlert("Mau so phai khac 0 !",this);                           
                       		 }
			else
			{		
				try{		
					gtsothunhat = Float.parseFloat(sothunhat.getString());
					gtsothuhai = Float.parseFloat(sothuhai.getString());	
				}			
				catch(Exception e){
					exception = true;
					filemanager.showAlert("Chuong trinh khong the tinh duoc so co dau phay va cac so qua lon!", this);
				}
				if(pheptinh.getSelectedIndex() == 0)
				{
					try{
						ketqua = gtsothunhat + gtsothuhai;
						ketquadaydu = Float.toString(ketqua);
					}			
					catch(Exception e){
						exception = true;
						filemanager.showAlert("Chuong trinh khong the tinh duoc so co dau phay va cac so qua lon!", this);
					}
				}
				else if(pheptinh.getSelectedIndex() == 1)
				{
					try{
						ketqua = gtsothunhat - gtsothuhai;
						ketquadaydu = Float.toString(ketqua);
					}			
					catch(Exception e){
						exception = true;
						filemanager.showAlert("Chuong trinh khong the tinh duoc so co dau phay va cac so qua lon!", this);
					}
				}
				else if(pheptinh.getSelectedIndex() == 2)
				{	
					try{
						ketqua = gtsothunhat * gtsothuhai;
						ketquadaydu = Float.toString(ketqua);
					}			
					catch(Exception e){
						exception = true;
						filemanager.showAlert("Chuong trinh khong the tinh duoc so co dau phay va cac so qua lon!", this);
					}
				}
				else
				{
					try{
						ketqua = gtsothunhat/gtsothuhai;
						ketquadaydu = Float.toString(ketqua);
					}			
					catch(Exception e){
						exception = true;
						filemanager.showAlert("Chuong trinh khong the tinh duoc so co dau phay va cac so qua lon!", this);
					}		
				}
				if(exception == false)
				{
					formketqua.append(gtsothunhat + " " + pheptinh.getString(pheptinh.getSelectedIndex()) + " " + gtsothuhai + " = " + ketquadaydu);				
					hienthi.setCurrent(formketqua);
				}
			}
		}
		if(c == tinhlai)
		{
			sothuhai.setString("");
			sothunhat.setString("");
			pheptinh.setSelectedIndex(0,true);
			hienthi.setCurrent(this);
			formketqua.deleteAll();
		}
		else if((c == tinhtiep))
		{
			sothuhai.setString("");
			sothunhat.setString("");
			pheptinh.setSelectedIndex(0,true);
			sothunhat.setString(Float.toString(ketqua));
			hienthi.setCurrent(this);
			formketqua.deleteAll();
		}
    }
}
